﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    ActiveUserFacility
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/7/10 21:20:07
// Update Time         :    2012/7/10 21:20:07
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.BLL.Basic.ExceptionImpl;
using Gfw.BLL.Basic.User.Enums;
using Gfw.Repositories.Account;
using Gfw.Repositories.Admin;
using Gfw.Repository;

namespace Gfw.BLL.Basic.Facilities
{
    /// <summary>
    /// 激活用户功能，按照分成的原则，这里不应该去操作股票账户，首先需要把用户的现金币转入电子币。
    /// 然后调用Stock里面的事务，完成激活
    /// </summary>
    public class ActiveUserFacility
    {
        private const double YearFee = 100;
        private const double AwardFee = 260;
        public const double ToEmoney = 200;

        private string _userId;

        public ActiveUserFacility(string userId)
        {
            if (string.IsNullOrEmpty(userId))
                throw new GfwVerifyException("转账的参数不正确！");
            _userId = userId;
        }

        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();
            try
            {
                ActUserAccountinformationRepository accountRep = new ActUserAccountinformationRepository();
                var account = accountRep.Get(_userId, work);

                if (account.Status != (int) UserStatusEnum.NotActive)
                    throw new GfwException("用户已经激活。");

                if (account.ElectronicMoney < 560)
                    throw new GfwVerifyException("余额不足");


                //首先缴纳年费
                AnnualFee annualFee = new AnnualFee(account, YearFee);
                annualFee.Execute(work);

                //扣除动态奖金
                AccruedAward accruedAward = new AccruedAward(account, AwardFee);
                accruedAward.Execute(work);

                //更改账户信息
                //account.ElectronicMoney -= 560;
                account.Status = (int) UserStatusEnum.WaitActive;
                accountRep.Update(account, work);

                //更改用户信息
                UsrUserRepository userRep = new UsrUserRepository();
                var user = userRep.Get(_userId, work);
                user.IsActivation = (int) UserStatusEnum.WaitActive;
                user.ActivationDate = DateTime.Now;
                userRep.Update(user, work);


                //剩下的工作，就是如何将200电子币，转换到股票币，并购买股票


                //以下是动态奖金的构成
                //直接推荐奖
                RecommendAward recommendAward = new RecommendAward(user.Recommender, 260*.15);
                recommendAward.Execute(work);

                //计算管理将
                ManagementAward maward = new ManagementAward(user.Id, 260);
                maward.Execute(work);

                //对碰奖


                //电子股

                if (unitOfWork == null)
                    work.Commit();
            }
            catch (Exception exp)
            {
                work.Rollback();
            }

        }
    }
}
